<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="blogInline" class="demo-form-inline">
      <el-form-item label="标题">
        <el-input v-model="blogInline.blogTitle" placeholder="产品经理成长手册"></el-input>
      </el-form-item>


      <el-form-item label="类型">
        <el-select v-model="blogInline.blogType" placeholder="艺术">
          <el-option v-for="item in typeList" :key="item.typeId" :label="item.typeName" :value="item.typeId"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="开始日期">
         <el-date-picker
            v-model="blogInline.begin"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
      </el-form-item>

      <el-form-item label="结束日期">
         <el-date-picker
            v-model="blogInline.end"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="resetData">清空</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增 -->
    <el-button type="success" @click="addBlog">新增</el-button>

    <!-- 列表 -->

    <el-table
        :data="blogData"
        border
        style="width: 100%">
      <el-table-column prop="blogTitle" label="标题" width="200" show-overflow-tooltip />
      <el-table-column prop="blogType" label="分类" sortable="custom" >
        <template slot-scope="scope">
          <span>{{typeMap[scope.row.blogType]}}</span>
        </template>
        
      </el-table-column>
      <!-- <el-table-column prop="blogType" label="分类" sortable="custom" /> -->
      
      <el-table-column prop="blogImage" label="图片" width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 100%;height: 100px"
            :src="scope.row.blogImage"
            :preview-src-list="[scope.row.blogImage]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="blogGoods" label="点赞数" width="50" sortable="custom" />
      <el-table-column prop="blogRead" label="阅读数" width="50" sortable="custom" />
      <el-table-column prop="blogCollection" label="收藏数" width="50" sortable="custom" />
      <el-table-column prop="blogComment" label="评论数" width="50" sortable="custom" />
      <el-table-column prop="blogSource" label="文章来源" />
      <el-table-column prop="gmtCreate" label="创建时间" sortable="custom" />
      <el-table-column prop="gmtModified" label="修改时间" sortable="custom" />
      <el-table-column prop="blogRemark" label="备注" width="200" show-overflow-tooltip />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作
              <i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleEdit(scope.row.blogId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="toRead(scope.row.blogId)">查看</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.blogId)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[10, 20, 50, 100,200]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalPages">
    </el-pagination>
    <!-- 添加弹窗 -->
    <el-dialog title="添加" :visible.sync="addDialog">
      <blog-add @closeAddDialog="closeAddDialog" @onSubmit="onSubmit" />
    </el-dialog>
    <!-- 阅读弹窗 -->
    <el-dialog title="阅读" :visible.sync="readDialog" width="50%">
      <div v-html="emitBlogData.blogContent" />
    </el-dialog>

    <!-- 修改弹窗 -->
    <el-dialog title="修改" :visible.sync="updateDialog">
      <blog-update :emitBlogData="emitBlogData" @closeUpdateDialog="closeUpdateDialog" @onSubmit="onSubmit" />
    </el-dialog>

  </div>

</template>


<script>
import BlogAdd from './blog-add'
import BlogUpdate from './blog-update'
import blogApi from '@/api/blog'
import typeApi from '@/api/type'
  export default {
      components:{
        BlogAdd,
        BlogUpdate
      },
      

      data() {
        return {
          typeMap:{},   //map集合，用来使得type的值可以显示中文，最小化性能损耗
          typeList:null,
          totalPages:'',
          current: 1,
          size: 10,
          blogInline: {
            blogTitle: '',
            blogType: '',
            begin: '',
            end: ''
          },
          emitBlogData:{},    //用它来传值
          blogData:[],
          addDialog : false,    //添加对话框
          updateDialog: false,//更新对话框
          readDialog  :false
        }
      },
      methods: {
          
        
        //map集合，用来使得type的值可以显示中文，最小化性能损耗
        getTypeToMap(){
          typeApi.getTypeMap().then(res=>{
            this.typeMap = res.data
          })
        },
        
        //新增
        addBlog(){
          this.addDialog = true
        },
        //获取所有博客类型接口
        getAllType(){
          typeApi.getTypeList()
          .then(res=>{
            this.typeList = res.data.typeList
            console.log("typeList",res.data.typeList)
          })
        },

        //提交数据，查询博客列表的方法
        onSubmit() {
          console.log('submit!',this.blogInline);
          blogApi.getBlogList(this.current,this.size,this.blogInline)
          .then(res=>{
            this.blogData = res.data.records
            this.totalPages = res.data.total
            console.log("执行成功",res.data)
          })
        },
        resetData(){
          this.blogInline = {}
          this.onSubmit()
        },
        closeAddDialog(){
          this.addDialog = false
        },
        closeUpdateDialog(){
          this.updateDialog = false
        },
        handleCurrentChange(val){
          this.current = val
          this.onSubmit()
        },
        handleSizeChange(val){
          this.size = val
          this.onSubmit()
        },
        handleEdit(blogId){
          blogApi.selectOneBlog(blogId).then(res=>{
            this.emitBlogData = res.data.data
            this.updateDialog = true
          })
          
        },
        toRead(blogId){
          blogApi.selectOneBlog(blogId).then(res=>{
            this.emitBlogData = res.data.data
            this.readDialog = true
          })
        },
        handleDelete(blogId){
          blogApi.deleteBlog(blogId)
          .then(res=>{
            this.$message.success(res.message)
            this.onSubmit()
            })
        }
      },
      created(){
        this.getAllType()
        this.onSubmit()
        this.getTypeToMap()
      }

    }
</script>
